<?php

/**
 * 
 * @author Administrator
 *
 */
class UeditorService extends Service{

	/**
	 * 上传附件和上传视频
	 * @param unknown $CONFIG
	 * @param unknown $action
	 * @return string
	 */
	public static function action_upload($CONFIG,$action){
		/* 上传配置 */
		$base64 = "upload";
		switch (htmlspecialchars($action)) {
			case 'uploadimage':
				$config = array(
				"pathFormat" => $CONFIG['imagePathFormat'],
				"maxSize" => $CONFIG['imageMaxSize'],
				"allowFiles" => $CONFIG['imageAllowFiles']
				);
				$fieldName = $CONFIG['imageFieldName'];
				break;
			case 'uploadscrawl':
				$config = array(
				"pathFormat" => $CONFIG['scrawlPathFormat'],
				"maxSize" => $CONFIG['scrawlMaxSize'],
				"allowFiles" => $CONFIG['scrawlAllowFiles'],
				"oriName" => "scrawl.png"
						);
						$fieldName = $CONFIG['scrawlFieldName'];
						$base64 = "base64";
						break;
			case 'uploadvideo':
				$config = array(
				"pathFormat" => $CONFIG['videoPathFormat'],
				"maxSize" => $CONFIG['videoMaxSize'],
				"allowFiles" => $CONFIG['videoAllowFiles']
				);
				$fieldName = $CONFIG['videoFieldName'];
				break;
			case 'uploadfile':
			default:
				$config = array(
				"pathFormat" => $CONFIG['filePathFormat'],
				"maxSize" => $CONFIG['fileMaxSize'],
				"allowFiles" => $CONFIG['fileAllowFiles']
				);
				$fieldName = $CONFIG['fileFieldName'];
				break;
		}
	
		/* 生成上传实例对象并完成上传 */
		$up = new PubUeUploader($fieldName, $config, $base64);
	
		/**
		 * 得到上传文件所对应的各个参数,数组结构
		 * array(
		 *     "state" => "",          //上传状态，上传成功时必须返回"SUCCESS"
		 *     "url" => "",            //返回的地址
		 *     "title" => "",          //新文件名
		 *     "original" => "",       //原始文件名
		 *     "type" => ""            //文件类型
		 *     "size" => "",           //文件大小
		 * )
		*/
	
		/* 返回数据 */
		return json_encode($up->getFileInfo());
	}
	
    /**
     * 获取已上传的文件列表
     * @param unknown $CONFIG
     * @param unknown $action
     * @return string
     */
	public static function action_list($CONFIG,$action){
		/* 判断类型 */
		switch ($action) {
			/* 列出文件 */
			case 'listfile':
				$CONFIG['allowFiles'] = $CONFIG['fileManagerAllowFiles'];
				$CONFIG['listSize'] = $CONFIG['fileManagerListSize'];
				$CONFIG['path'] = $CONFIG['fileManagerListPath'];
				break;
				/* 列出图片 */
			case 'listimage':
			default:
				$CONFIG['allowFiles'] = $CONFIG['imageManagerAllowFiles'];
				$CONFIG['listSize'] = $CONFIG['imageManagerListSize'];
				$CONFIG['path'] = $CONFIG['imageManagerListPath'];
		}
		$pubUeLister = new PubUeLister($CONFIG);
		$result = $pubUeLister->getList();		
		return json_encode($result);
	}
	
	
	/**
	 * 抓取远程图片
	 * @param unknown $CONFIG
	 * @return string
	 */
	public static function action_crawler($CONFIG){
		set_time_limit(0);		
		/* 上传配置 */
		$config = array(
				"pathFormat" => $CONFIG['catcherPathFormat'],
				"maxSize" => $CONFIG['catcherMaxSize'],
				"allowFiles" => $CONFIG['catcherAllowFiles'],
				"oriName" => "remote.png"
		);
		$fieldName = $CONFIG['catcherFieldName'];
		
		/* 抓取远程图片 */
		$list = array();
		if (isset($_POST[$fieldName])) {
			$source = $_POST[$fieldName];
		} else {
			$source = $_GET[$fieldName];
		}
		foreach ($source as $imgUrl) {
			$item = new Uploader($imgUrl, $config, "remote");
			$info = $item->getFileInfo();
			array_push($list, array(
			"state" => $info["state"],
			"url" => $info["url"],
			"size" => $info["size"],
			"title" => htmlspecialchars($info["title"]),
			"original" => htmlspecialchars($info["original"]),
			"source" => htmlspecialchars($imgUrl)
			));
		}
		
		/* 返回抓取数据 */
		return json_encode(array(
				'state'=> count($list) ? 'SUCCESS':'ERROR',
				'list'=> $list
		));
	}

}